﻿<#
    Script     : LoopbackConnection
    Author     : Riwut Libinuko
    Blog       : http://blog.libinuko.com
    Copyright© 2011, Riwut Libinuko (IdeasFree - cakriwut@gmail.com). All Rights Reserved.    
#>

function Set-BackConnectionHost
{
<#
.SYNOPSIS
Append or Remove BackConnectionHost registry entry, HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0.
		
.DESCRIPTION
This function will Append or Remove BackConnectionhost registry entry based on specified hostnames. You can specify array of hostnames, or comma-separated hostnames.
    
.INPUTS
None. You can not pipe objects to Remove-BackConnectionHost
	
.OUTPUTS
None. No output from Remove-BackConnectionHost
		
.PARAMETER hostnames
MANDATORY parameter to hostnames to be appended. For example $hostnames="foo.com,www.foo.org" or $hostnames= @("foo.com","www.foo.org") 

.PARAMETER option
OPTIONAL parameter to option, valid options "Append" and "Override". Default: "Append". Append will append new hostnames to existing one, 
while Override will remove and replace existing BackHostConnection with the new hostnames. 
	
        	
.EXAMPLE
    PS>  Remove-BackConnectionHost "foo.com,www.foo.org"
			

	Description
	-----------
	Remove foo.com and www.foo.org from BackConnectionHost registry entry. 
    
.EXAMPLE
    PS>  Remove-BackConnectionHost  @("foo.com","www.foo.org")
			

	Description
	-----------
	Remove foo.com and www.foo.org from BackConnectionHost registry entry. 
    		            
.LINK
    Author blog  : IdeasForFree  (http://blog.libinuko.com)
.LINK
    Author email : cakriwut@gmail.com
#>
   param(
      [Parameter(Mandatory=$true,Position=0)] 
      $hostnames,
      [ValidateSet("Append","Override")]
      $option="Append"
      )
    
   if(!($hostnames -is [system.array]))
   { 
      $hostnames = $hostnames -split ","
   }
   
   # Avoid duplicate
   Remove-BackConnectionHost $hostnames > $null 
      
   $original = get-itemproperty HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0
   if($original.BackConnectionHostNames)
   {  
      if($option -eq "Append")
      {    
        $hostnames += $original.BackConnectionHostNames           
      }
      Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 -Name "BackConnectionHostNames" -Value $hostnames 
   } else {      
      New-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 -Name "BackConnectionHostNames" -PropertyType multistring  -Value $hostnames 
   }
   
   Write-Host "Modification completed."
   Write-Host "Changes will be effective after reboot. Please schedule server restart!"
}

function Get-BackConnectionHost
{
<#
.SYNOPSIS
Display BackConnectionHost registry entry, HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0.
		
.DESCRIPTION
This function will list BackConnectionhost registry entry based.
    
.INPUTS
None. 
	
.OUTPUTS
None. 		
    		            
.LINK
    Author blog  : IdeasForFree  (http://blog.libinuko.com)
.LINK
    Author email : cakriwut@gmail.com
#>
    $entries = (get-itemproperty HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 -ErrorAction SilentlyContinue).BackConnectionHostNames
    return $entries 
}

function Remove-BackConnectionHost
{
<#
.SYNOPSIS
Remove BackConnectionHost registry entry, HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0.
		
.DESCRIPTION
This function will remove BackConnectionhost registry entry based on specified hostnames. You can specify array of hostnames, or comma-separated hostnames.
    
.INPUTS
None. You can not pipe objects to Remove-BackConnectionHost
	
.OUTPUTS
None. No output from Remove-BackConnectionHost
		
.PARAMETER hostnames
MANDATORY parameter to hostnames to be removed. For example $hostnames="foo.com,www.foo.org" or $hostnames= @("foo.com","www.foo.org") 
	
        	
.EXAMPLE
    PS>  Remove-BackConnectionHost "foo.com,www.foo.org"
			

	Description
	-----------
	Remove foo.com and www.foo.org from BackConnectionHost registry entry. 
    
.EXAMPLE
    PS>  Remove-BackConnectionHost  @("foo.com","www.foo.org")
			

	Description
	-----------
	Remove foo.com and www.foo.org from BackConnectionHost registry entry. 
    		            
.LINK
    Author blog  : IdeasForFree  (http://blog.libinuko.com)
.LINK
    Author email : cakriwut@gmail.com
#>
   param (
     [Parameter(Mandatory=$true,Position=0)] 
     $hostnames
   )
   
   if(!($hostnames -is [system.array]))
   { 
      $hostnames = $hostnames -split ","
   }
   
   $original = get-itemproperty HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0
   if($original.BackConnectionHostNames)
   {     
      $hostnameList = $original.BackConnectionHostNames |? { $hostnames -notcontains $_ }      
      
      if(!($hostnames |? { $original.BackConnectionHostNames -contains $_ }))
      {
        Write-Host "Can not find hostnames in the registry. No removal has been committed!" 
      } else {        
        Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 -Name "BackConnectionHostNames" -Value $hostnameList 
        Write-Host "Modification has been applied!" 
      }
      
   } else {
      Write-Host "Can not find registry entry. No removal has been committed!"
   }
}
